var scheduledDates = new Array();

jQuery(document).ready(function() {
	renderScheduledDates();
	
	jQuery("#wptravel_addScheduleButton").click(function(e){
		
		if (!validateDateForm()){
			return;
		}
		
		scheduledDates.push({startDate:     jQuery("#wptravel_startDate").val(),
							 endDate:       jQuery("#wptravel_endDate").val(),
							 price:         jQuery("#wptravel_price").val(),
							 currency:      jQuery("#wptravel_currency").val(),
							 accommodation: jQuery("#wptravel_accommodation").val(),
							 air:           new Boolean(jQuery("#wptravel_air").attr("checked")),
							 closed:        new Boolean(jQuery("#wptravel_closed").attr("checked")),
							 guaranteed:    new Boolean(jQuery("#wptravel_guaranteed").attr("checked")),
							 limited:       new Boolean(jQuery("#wptravel_limitedAvailability").attr("checked"))});
		
		resetDatesForm();
		renderScheduledDates();
	});
	
});

function validateDateForm() {
	valid = true;
	if (jQuery("#wptravel_startDate").val() == "") valid = false;
	if (jQuery("#wptravel_endDate").val() == "") valid = false;
	if (jQuery("#wptravel_price").val() == "") valid = false;
	
	return valid;
}

function resetDatesForm() {
	jQuery("#wptravel_startDate").val("");
	jQuery("#wptravel_endDate").val("");
	jQuery("#wptravel_DstartDate").val("");
	jQuery("#wptravel_DendDate").val("");
	jQuery("#wptravel_price").val("");
	jQuery("#wptravel_accommodation").attr('selectedIndex', 0);
	jQuery("#wptravel_currency").attr('selectedIndex', 0);
	jQuery("#wptravel_air").removeAttr('checked');
	jQuery("#wptravel_closed").removeAttr('checked');
	jQuery("#wptravel_guaranteed").removeAttr('checked');
	jQuery("#wptravel_limitedAvailability").removeAttr('checked');
	
}

function renderScheduledDates() {
	
	if (scheduledDates.length == 0){
		html ="NO DATES ADDED";
	} else {
		html = "<table>";
		//html += "<thead>";
		//html += "<tr>";
		//html += "<td>Start</td><td>End</td><td>Price</td><td>Closed</td><td>Guaranteed</td><td>Limited</td>";
		//html += "</tr>";
		//html += "</thead>";
		html += "<tbody>";
		
		for (var i=0; i < scheduledDates.length; i++){
			row = scheduledDates[i];
			
			formString = row.startDate + ":" + row.endDate + ":" + row.price + ":" + row.currency + ":" + row.closed + ":" + row.guaranteed + ":" + row.limited + ":" + row.accommodation + ":" + row.air;
			
			html += "<tr>";
			html += "<td>";
			html += '<input type="hidden" name="wptravel_scheduledDate[]" value="' + formString + '" />';
			html += "Start Date: " + row.startDate + "<br />";
			html += "End Date: " + row.endDate + "<br />";
			html += "Price: $" + row.price + row.currency + "<br />";
			html += "Accommodation Type: " + row.accommodation + "<br />";

			if (row.air == true){
				html += "Air Included<br />";
			} else {
				html += "Air NOT Included<br />";
			}
			
			if (row.closed == true){
				html += "Closed<br />";
			}
			
			if (row.guaranteed == true){
				html += "Guaranteed<br />";
			} else {
				html += "NOT Guaranteed<br />";
			}
			
			if (row.limited == true){
				html += "Limited Availability<br />";
			}
			
			html += '<input type="button" value="remove this date" onclick="removeScheduledDate(' + i + ')" />';
			html += '<hr />';
			html += "</td>";
			html += "</tr>";
		}
		
		html += "</tbody>";
		html += "</table>";
	}
	
	jQuery("#wptravel_scheduledDates").html(html);
}

function removeScheduledDate(i)
{
	scheduledDates.splice(i, 1);
	renderScheduledDates();
}
